package ltesim.sdkManager;

import java.util.ArrayList;

import ltesim.sdkManager.listener.AddButtonListener;
import ltesim.sdkManager.listener.RemoveButtonListener;
import ltesim.sdkManager.sdklist.SDKList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.ViewPart;

/**
 * SDK Manager Class
 * 
 * @author xirka
 * 
 */
public class SDKManager extends ViewPart {
	private FormToolkit toolkit;
	private ScrolledForm form;
	private SDKList mySDK;

	private Table table;

	/**
	 * Constructor
	 */
	public SDKManager() {
		super();
		mySDK = new SDKList();
	}

	/**
	 * Clear all table content
	 */
	public void clearTableContent() {
		table.removeAll();
	}

	/**
	 * Remove the selected row on SDK table
	 */
	public void removeSelected() {
		int idx = table.getSelectionIndex();
		mySDK.removeVersion(idx);
		mySDK.writeConfig();
		clearTableContent();
		resetTableContent();
	}

	/**
	 * Reset table content by reading the SDKManager config file on .metadata folder
	 */
	public void resetTableContent() {
		ArrayList<String> versions = mySDK.getVersionList();
		ArrayList<String> dirs = mySDK.getDirList();

		for (int i = 0; i < versions.size(); ++i) {
			String version = versions.get(i);
			String dir = dirs.get(i);
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(0, version);
			item.setText(1, dir);
		}
	}

	@Override
	public void createPartControl(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);

		toolkit.decorateFormHeading(form.getForm());
		form.setFont(new Font(null, "Helvetica", 13, SWT.BOLD | SWT.ITALIC));
		form.setText("SDK Manager");

		Composite body = form.getBody();
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		body.setLayout(layout);

		table = new Table(body, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		GridData tableLayout = new GridData(SWT.FILL, SWT.FILL, true, true);
		tableLayout.heightHint = 200;
		tableLayout.verticalSpan = 2;
		table.setLayoutData(tableLayout);

		String[] tableTitle = { "Version", "Directory" };
		for (String title : tableTitle) {
			TableColumn col = new TableColumn(table, SWT.NONE);
			col.setText(title);
		}
		table.getColumn(0).setWidth(100);
		table.getColumn(1).setWidth(250);

		resetTableContent();

		GridData buttonLayout = new GridData(GridData.HORIZONTAL_ALIGN_FILL
				| GridData.VERTICAL_ALIGN_BEGINNING);
		buttonLayout.widthHint = 100;
		Button addButton = toolkit.createButton(body, "Add", SWT.PUSH);
		addButton.setLayoutData(buttonLayout);
		addButton.addSelectionListener(new AddButtonListener(this, parent
				.getDisplay()));

		Button removeButton = toolkit.createButton(body, "Remove", SWT.PUSH);
		removeButton.setLayoutData(buttonLayout);
		removeButton.addSelectionListener(new RemoveButtonListener(this));

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	public SDKList getMySDK() {
		return mySDK;
	}

	public void setMySDK(SDKList mySDK) {
		this.mySDK = mySDK;
	}

	public Table getTable() {
		return table;
	}

	public void setTable(Table table) {
		this.table = table;
	}

}